package com.jy.study;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jy.study.entity.Person;
import com.jy.study.entity.Student;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Classname ObjectUtils
 * @Description 对象工具类
 * @Date 2019/9/27 14:54
 * @Created by  jiaoyong
 */
public class ObjectUtils {
    public static void main(String[] args) {
        //准备数据
        Person person1 = new Person();
        Person person2 = person1;
        String str1="";
        String str2="";

        /*比较两个对象是否相等，相等需满足以下条件之一：
        1.obj1 == null && obj2 == null
        2.obj1.equals(obj2)*/
        //equal1:true
        boolean equal1 = ObjectUtil.equal(str1, str2);
        //equal2:true
        boolean equal2 = ObjectUtil.equal(person1, person2);

        //准备数据
        StringBuilder builder = StrUtil.builder(5).append("d").append("e");
        ArrayList<Integer> list = CollUtil.newArrayList(1, 2, 3);
        Object[] array = ArrayUtil.newArray(3);
        HashMap<Object, Object> hashMap = CollUtil.newHashMap(6);
        /*计算对象长度，如果是字符串调用其length函数，集合类调用其size函数，数组调用其length属性，
        其他可遍历对象遍历计算长度,支持的类型包括： CharSequence Map Iterator Enumeration Array*/
        //length1=2
        int length1 = ObjectUtil.length(builder);
        //length2 = 3
        int length2 = ObjectUtil.length(list);
        //length3 = 3
        int length3 = ObjectUtil.length(array);
        //length4 = 0
        int length4 = ObjectUtil.length(hashMap);


        /*对象中是否包含元素。支持的对象类型包括： String Collection Map Iterator Enumeration Array*/
        //dFlag = true
        boolean dFlag = ObjectUtil.contains(builder.toString(), "d");
        //contains = false
        boolean contains = ObjectUtil.contains(list, 4);
        //arrayFlag = true
        boolean arrayFlag = ObjectUtil.contains(array, null);
        //mapFlag = false
        boolean mapFlag = ObjectUtil.contains(hashMap, null);


        /*判断是否为null*/
        //strFlag = false
        boolean strFlag = ObjectUtil.isNull(builder);
       //arrFlag = true
        boolean arrFlag = ObjectUtil.isNotNull(array);
        boolean notEmpty = ArrayUtil.isNotEmpty(array);
        System.out.println("notEmpty = " + notEmpty);


    }
}
